package com.vlin.message.handler.tuya;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vlin.entity.device.DeviceStatu;
import com.vlin.mapper.device.DeviceStatusMapper;

@Component
public class DataUpdateHandler extends AbstractTuyaMessageHandler {

	@Autowired
	private DeviceStatusMapper statuDao;
	
	@Override
	public boolean doSupport(JSONObject object) {
		
		return object.containsKey("dataId");
	}

	@Override
	public void doHandle(JSONObject object) {
		//DeviceStatu statu = new DeviceStatu();
		String devId = object.getString("devId");
		
		JSONArray status = object.getJSONArray("status");
		for(int i = 0 ; i < status.size(); i ++) {
			DeviceStatu statu = status.getObject(i, DeviceStatu.class);
			statu.setDid(devId);
			int result = statuDao.update(statu);
			if(result <= 0) {
				statuDao.insert(statu);
			}
		}
	}

}
